home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / copy / RCS / copy.c,v < prev   
Encoding:
Text File  |  1992-07-17  |  2.6 KB  |  149 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  srv030:1.2 srv027:1.2 srv026:1.2 srv024:1.2 srv021:1.2 srv018:1.2 srv014:1.2 srv010:1.2 srv008:1.2 srv007:1.2 srv006:1.2 srv004:1.2;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     91.12.12.22.33.09;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     91.12.01.22.44.13;  author kupfer;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @Trivial "cp".
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @Allow use of either malloc or vm_allocate to allocate the buffer.
  28. @
  29. text
  30. @/* Simple file copy program.  */
  31.  
  32. #ifndef lint
  33. static char rcsid[] = "$Header$";
  34. #endif
  35.  
  36. #include <sprite.h>
  37. #include <mach.h>
  38. #include <mach_error.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <sys/fcntl.h>
  42. #include <test.h>
  43. #include <unistd.h>
  44.  
  45. /* #define USE_MALLOC    1 */
  46.  
  47. static void CopyFile();
  48.  
  49. int
  50. main(argc, argv)
  51.     int argc;
  52.     char *argv[];
  53. {
  54.     if (argc != 3) {
  55.     Test_PutMessage("usage: copy from to\n");
  56.     exit(1);
  57.     }
  58.     CopyFile(argv[1], argv[2]);
  59.     
  60.     return 0;
  61. }
  62.  
  63. static void
  64. CopyFile(fromFileName, toFileName)
  65.     char *fromFileName;
  66.     char *toFileName;
  67. {
  68.     int fromFd;
  69.     int toFd;
  70.     Address buffer;
  71.     int nChars;
  72. #ifndef USE_MALLOC
  73.     kern_return_t kernStatus;
  74. #endif
  75.     int bufferSize;        /* number of bytes in buffer */
  76.  
  77.     fromFd = open(fromFileName, O_RDONLY);
  78.     if (fromFd < 0) {
  79.     perror(fromFileName);
  80.     exit(1);
  81.     }
  82.     toFd = open(toFileName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  83.     if (toFd < 0) {
  84.     perror(toFileName);
  85.     exit(1);
  86.     }
  87. #ifdef USE_MALLOC
  88.     bufferSize = 2 * vm_page_size;
  89.     buffer = malloc(bufferSize);
  90.     if (buffer == NULL) {
  91.     Test_PutMessage("Couldn't malloc buffer\n");
  92.     exit(1);
  93.     } else {
  94.     Test_PutMessage("buffer at ");
  95.     Test_PutHex(buffer);
  96.     Test_PutMessage("\n");
  97.     }
  98. #else
  99.     bufferSize = 2 * vm_page_size;
  100.     buffer = 0;
  101.     kernStatus = vm_allocate(mach_task_self(), (vm_address_t *)&buffer, 
  102.                  bufferSize, TRUE);
  103.     if (kernStatus != KERN_SUCCESS) {
  104.     Test_PutMessage("Couldn't allocate buffer: ");
  105.     Test_PutDecimal(kernStatus);
  106.     Test_PutMessage("\n");
  107.     exit(1);
  108.     }
  109. #endif
  110.  
  111.     while ((nChars = read(fromFd, buffer, bufferSize)) > 0) {
  112.     Test_PutMessage("Read ");
  113.     Test_PutDecimal(nChars);
  114.     Test_PutMessage("\n");
  115.     if (write(toFd, buffer, nChars) != nChars) {
  116.         Test_PutMessage("short write.\n");
  117.         exit(1);
  118.     }
  119.     }
  120.     if (nChars < 0) {
  121.     perror("read");
  122.     exit(1);
  123.     }
  124. }
  125. @
  126.  
  127.  
  128. 1.1
  129. log
  130. @Initial revision
  131. @
  132. text
  133. @d3 4
  134. d16 2
  135. d41 1
  136. a41 1
  137.     vm_address_t buffer;
  138. d43 1
  139. d45 2
  140. d58 13
  141. d72 2
  142. a73 1
  143.     kernStatus = vm_allocate(mach_task_self(), &buffer, vm_page_size, TRUE);
  144. d80 1
  145. d82 4
  146. a85 1
  147.     while ((nChars = read(fromFd, buffer, vm_page_size)) > 0) {
  148. @
  149.